package com.habitsoft.glassfish.dashboard.client;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.habitsoft.kiyaa.util.AsyncCallbackFactory;
import com.habitsoft.kiyaa.util.AsyncCallbackGroup;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Dashboard implements EntryPoint {

    LogServiceAsync logs;
    StatServiceAsync stats;
    
    Timer refreshTimer = new Timer() {
        public void run() {
            refresh();
        };
    };
    private LogView logView;
    private StatView statView;
    private LogServiceCachingProxy logsCachingProxy;
    private LogServiceAsync logsNoCache;
    
    /**
     * This is the entry point method.
     */
    @SuppressWarnings("unchecked")
    public void onModuleLoad() {
        AsyncCallbackFactory.setDefaultFactory(new AsyncCallbackFactory() {
            public AsyncCallback newInstance() {
                return new AsyncCallback() {
                    public void onFailure(Throwable arg0) {
                        Log.error("Uncaught exception", arg0);
                    }
                    public void onSuccess(Object arg0) {
                    }
                };
            }
        });
        
        logs = logsNoCache = GWT.create(LogService.class);
        logs = logsCachingProxy = new LogServiceCachingProxy(logs);
        logs.listLogFiles(new AsyncCallback<String[]>() {
            public void onFailure(Throwable arg0) {
                GWT.log("Failed to list log files.", null);
            }
            public void onSuccess(String[] arg0) {
                GWT.log("Got "+arg0.length+" log file names", null);
                for(String name : arg0) {
                    GWT.log("  "+name, null);
                }
            }
        });
        stats = GWT.create(StatService.class);
        
        
        addStatView();
        addLogView();
        refresh();
        refreshTimer.scheduleRepeating(10000);
    }

    public void flushLogCounts() {
        logsCachingProxy.counts.clear();
    }
    
    private void addLogView() {
        logView = GWT.create(LogView.class);
        logView.setController(this);
        RootPanel.get().add(logView.getViewWidget());
    }

    private void addStatView() {
        statView = GWT.create(StatView.class);
        statView.setController(this);
        RootPanel.get().add(statView.getViewWidget());
    }
    
    @SuppressWarnings("unchecked")
    private void refresh() {
        AsyncCallbackGroup group = new AsyncCallbackGroup();
        logView.load(group.member());
        statView.load(group.member());
        group.ready(AsyncCallbackFactory.defaultNewInstance("Refresh Dashboard From Timer"));
    }

    public LogServiceAsync getLogs() {
        return logs;
    }

    public void setLogs(LogServiceAsync logs) {
        this.logs = logs;
    }

    public StatServiceAsync getStats() {
        return stats;
    }

    public void setStats(StatServiceAsync stats) {
        this.stats = stats;
    }

    public LogServiceAsync getLogsNoCache() {
        return logsNoCache;
    }
}
